﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;

namespace FileDFS.Utils.PathConfig
{
    /*
     * Coder:Tieria
     * WriteTime:2015.5.14
     * FileName:CatalogHandler.cs
     * ReMark:文件目录的相关操作
     */
    public class CatalogHandler
    {
        public static CatalogHandler GetInstance()
        {
            return new CatalogHandler();
        }

        #region 获取目录配置的根节点
        /// <summary>
        /// 获取目录配置的根节点配置
        /// </summary>
        /// <returns></returns>
        public AllPathConfig GetAllFilePath()
        {
            AllPathConfig config = (AllPathConfig)ConfigurationManager.GetSection("AllPathConfig");
            return config;
        }
        #endregion

        #region 获取配置文件下所有根目录的集合
        /// <summary>
        /// 获取配置文件下所有根目录的集合
        /// </summary>
        /// <returns></returns>
        public List<PathOption> GetAllOptions()
        {
            AllPathConfig config = GetAllFilePath();
            List<PathOption> allList = new List<PathOption>();
            var query = (config.imagePathConfig.publicPath.optionsCollection.Cast<PathOption>()).Union(config.imagePathConfig.privatePath.optionsCollection.Cast<PathOption>()).Union(config.filePathConfig.publicPath.optionsCollection.Cast<PathOption>()).Union(config.filePathConfig.privatePath.optionsCollection.Cast<PathOption>()).ToList();
            return query;
        } 
        #endregion

        #region 根据传递的枚举类型获取 PathOption
        /// <summary>
        /// 根据传递的枚举类型获取PathOption
        /// </summary>
        /// <param name="getEnum"></param>
        /// <returns></returns>
        public List<PathOption> GetRootOptionList(OptionGetEnum getEnum)
        {
            AllPathConfig config = GetAllFilePath();
            IEnumerable<PathOption> ieQuery;
            switch (getEnum)
            {
                case OptionGetEnum.ImagePublic:
                default:
                    ieQuery = from p in config.imagePathConfig.publicPath.optionsCollection.Cast<PathOption>() select p;
                    break;
                case OptionGetEnum.ImagePrivate:
                    ieQuery = from p in config.imagePathConfig.privatePath.optionsCollection.Cast<PathOption>() select p;
                    break;
                case OptionGetEnum.FilePrivate:
                    ieQuery = from p in config.filePathConfig.privatePath.optionsCollection.Cast<PathOption>() select p;
                    break;
            }
            return ieQuery.ToList();
        }
        #endregion

        #region 将PathOption转换成PathDetail
        /// <summary>
        /// 将PathOption转换成PathDetail
        /// </summary>
        /// <param name="pathOption">pathOption</param>
        /// <returns>PathDetail</returns>
        public PathDetail GetDetailByPathOption(PathOption pathOption)
        {
            PathDetail pathDetail = new PathDetail();
            pathDetail.Virtual_FullPath = PathHelper.CombineRelativePath(pathOption.VirtualPath);
            pathDetail.Absolute_FullPath = Path.Combine(PathHelper.CombineDriverAbsolute(pathOption.AbsoluteDriver, pathOption.AbsolutePath));

            return pathDetail;
        }
        #endregion

        #region 根据枚举类型获取当前存储路径的详情
        /// <summary>
        /// 根据枚举类型获取当前存储路径的详情
        /// </summary>
        /// <param name="getEnum"></param>
        /// <returns></returns>
        public PathDetail GetCurrentPath(OptionGetEnum getEnum)
        {
            List<PathOption> rootOptionList = GetRootOptionList(getEnum);
            //随机获取当前的根目录
            Random rd = new Random();
            int currentIndex = rd.Next(0, rootOptionList.Count);
            PathOption rootOption = rootOptionList[currentIndex];
            PathDetail rootDetail = GetDetailByPathOption(rootOption);
            //创建根目录
            Directory.CreateDirectory(rootDetail.Absolute_FullPath);
            string[] pathStr = new string[] { DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), DateTime.Now.Day.ToString() };

            PathOption currentOption = new PathOption();
            currentOption.AbsoluteDriver = rootOption.AbsoluteDriver;
            currentOption.VirtualPath = PathHelper.CombineRelativePath(rootOption.VirtualPath, pathStr[0], pathStr[1], pathStr[2]);
            currentOption.AbsolutePath = Path.Combine(rootOption.AbsolutePath, pathStr[0], pathStr[1], pathStr[2]);
            PathDetail currentDetail = GetDetailByPathOption(currentOption);
            //创建该目录
            Directory.CreateDirectory(currentDetail.Absolute_FullPath);
            return currentDetail;

        } 
        #endregion

        #region 根据虚拟路径获取名称
        public string GetVirtualName(string virtualFullPath)
        {
            string virtualName = string.Empty;
            if (virtualFullPath.Length > 0)
            {
                string[] allPaths = virtualFullPath.Split('/');
                foreach (string item in allPaths)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        virtualName = item;
                        break;
                    }
                }
            }
            return virtualName;
        } 
        #endregion

        #region 根据虚拟全路径获取当前配置文件下的根目录
        /// <summary>
        /// 根据虚拟全路径获取当前配置文件下的根目录
        /// </summary>
        /// <param name="virtualFullPath"></param>
        /// <returns></returns>
        public PathDetail GetDetailByVirtual(string virtualFullPath)
        {
            string absolutePath = string.Empty;
            PathDetail detail = new PathDetail();
            if (virtualFullPath.Length > 0)
            {
                //先根据虚拟的全路径获取虚拟目录名称
                string virtualName = GetVirtualName(virtualFullPath);
                //根据虚拟目录名称获取某一条详细的配置信息
                List<PathOption> optionList = GetAllOptions();
                PathOption rootOption = optionList.Where(x => x.VirtualPath.Equals(virtualName)).FirstOrDefault();
                //根目录的详情
                //    PathDetail rootDetail = GetDetailByPathOption(rootOption);
                detail.Virtual_FullPath = virtualFullPath;
                //绝对盘符+根目录 D:\xxx
                absolutePath = PathHelper.CombineDriverAbsolute(rootOption.AbsoluteDriver, rootOption.AbsolutePath);
                string[] pathParts = virtualFullPath.Split('/');
                string fileFullName = pathParts[pathParts.Length - 1];
                //将虚拟全路径进行分割,循环拼接出 绝对路径根目录以外的路径
                foreach (var item in pathParts)
                {
                    if (!string.IsNullOrEmpty(item) && item != fileFullName && item != virtualName)
                    {
                        absolutePath = Path.Combine(absolutePath,item);
                    }
                }
                detail.Absolute_FullPath = Path.Combine(absolutePath, fileFullName);

            }
            return detail;
        } 
        #endregion


    }
}
